﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rem.Objects.Forms
{
    public partial class ReminderForm : BaseForm
    {
        public UcReminder ReminderControl { get { return ucReminder1; } }
        internal UcReminder ucReminder1;
        public const string TextTemplate = "提醒#{0}-{1}";
        
        public ReminderForm(Reminder reminder)
            : this(string.Format(TextTemplate, reminder.Id, reminder.ReminderCategory))
        {
            this.ucReminder1.Data = reminder;
        }

        public ReminderForm()
            : this("新建提醒")
        { }

        public ReminderForm(string text) : this(text, null) { }

        public ReminderForm(string text, Contract contract)
        {
            InitializeComponent();
            if (contract != null)
            {
                this.ucReminder1 = new UcReminder(contract);
                
            }
            else
            {
                this.ucReminder1 = new UcReminder();
            }
            this.ucReminder1.Dock = DockStyle.Fill;
            this.ucReminder1.Parent = this;
            this.Text = text;
            this.ucReminder1.Saved += new EventHandler(ucReminder1_Saved);
            this.ucReminder1.Deleted += (sender, arg) =>
            {
                this.Close();
            };
        }

        public void ucReminder1_Saved(object sender, EventArgs e)
        {
            Reminder reminder = sender as Reminder;
            this.Text = string.Format(TextTemplate, reminder.Id, reminder.ReminderCategory);
        }

        public event EventHandler GenerateReminders
        {
            add
            {
                this.ucReminder1.GenerateReminders += value;
            }
            remove
            {
                this.ucReminder1.GenerateReminders -= value;


            }
        }

        public override BaseObjectTypes BaseType
        {
            get
            {
                return BaseObjectTypes.Reminder;
            }
        }

        

        public static void NewPeriodRemind()
        {
            ReminderForm form = new ReminderForm("生成周期提醒", null);
            form.ReminderControl.periedgroupBox.Visible = true;
            form.Dock = DockStyle.Fill;
            form.GenerateReminders += (source, arg) =>
            {
                IEnumerable<Reminder> reminders = source as IEnumerable<Reminder>;
                if (reminders != null)
                {
                    QueryForm.OpenReminderQuery(0, "周期提醒", reminders);
                }
            };
        }
    }
}
